#coding:utf-8
'''
create on 2017-07-13

@author: sandy
'''

import os
import json
import logging
from pycommon import Utils
from pycommon import RemoteManager

class Config(object):
    
    def __init__(self):
        self.rundir = os.path.dirname(__file__)
        self.log_db_name = u''
        self.log_db_host = u"127.0.0.1"
        self.log_db_port = 3306
        self.log_db_user = u"root"
        self.log_db_passwd = u"1111"
        self.log_db_linkcount = 2

        self.server_ip = u"127.0.0.1"
        self.server_port = 0
        self.max_client = 0

    
    def onRemoteConfig(self, cfgname, jf):
        ctype,cname = cfgname.split(u".")
        if ctype==u'server'+unicode(RemoteManager.instance.sid) and cname==u'serverconfig':
            self.loadFromJson(jf)
       
    def loadFromJson(self, cf):
        logging.debug(u"logserver parse serverconfig.json")
        dbstr = cf.get("log_dbe")
        if dbstr==None:
            dbstr = cf.get("log_db")
        else:
            dbstr = Utils.dencryptDbStr(dbstr)
        dbparams = dbstr.split(u',')
        self.log_db_name = dbparams[2]
        self.log_db_host = dbparams[0]
        self.log_db_port = int(dbparams[1])
        self.log_db_user = dbparams[3]
        self.log_db_passwd = dbparams[4]
        self.log_db_linkcount = int(dbparams[5]) 

        self.server_ip = cf.get("server_ip")
        self.server_port = cf.get("server_port") 
        self.max_client = cf.get("max_client")
        
   
instance = Config()